package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduSchoolMapper;
import com.zenith.edu.po.domain.EduSchool;
import com.zenith.edu.po.dto.EduSchoolDTO;
import com.zenith.edu.po.vo.EduSchoolVO;
import com.zenith.edu.service.EduSchoolService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduSchoolServiceImpl extends ServiceImpl<EduSchoolMapper, EduSchool> implements EduSchoolService {
    
    @Override
    public EduSchoolVO createSchool(EduSchoolDTO dto) {
        EduSchool school = new EduSchool();
        BeanUtils.copyProperties(dto, school);
        this.save(school);
        
        EduSchoolVO vo = new EduSchoolVO();
        BeanUtils.copyProperties(school, vo);
        return vo;
    }
    
    @Override
    public EduSchoolVO getSchoolById(Long id) {
        EduSchool school = this.getById(id);
        if (school == null) {
            return null;
        }
        
        EduSchoolVO vo = new EduSchoolVO();
        BeanUtils.copyProperties(school, vo);
        return vo;
    }
    
    @Override
    public EduSchoolVO updateSchool(EduSchoolDTO dto) {
        EduSchool school = new EduSchool();
        BeanUtils.copyProperties(dto, school);
        this.updateById(school);
        
        EduSchoolVO vo = new EduSchoolVO();
        BeanUtils.copyProperties(school, vo);
        return vo;
    }
    
    @Override
    public boolean deleteSchool(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduSchoolVO> getSchoolList() {
        List<EduSchool> schoolList = this.list();
        return schoolList.stream().map(school -> {
            EduSchoolVO vo = new EduSchoolVO();
            BeanUtils.copyProperties(school, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}